package game.controller;

import game.gui.MainScreen;

import javax.swing.*;

import game.gui.info.NationInfoFrame;
import lobby.controller.Nation;
import shared.commCards.CommCardFrame;

import java.awt.*;

/**
 * DisplayController.java  Date Created: Oct 24, 2012
 *
 * Purpose: To control what is displayed on the screen.
 *
 * Description: This class shall control what the user sees on screen.
 *
 * @author Chrisb
 */
public class DisplayController {
    public final static Color FRENCH_COLOR = Color.BLUE;
    public final static Color BRITISH_COLOR = Color.RED;
    public final static Color PRUSSIAN_COLOR = Color.PINK;
    public final static Color RUSSIAN_COLOR = Color.ORANGE;
    public final static Color OTTOMAN_COLOR = Color.YELLOW;
    public final static Color AUSTRIAN_COLOR = Color.GREEN;
    public final static Color SPANISH_COLOR = Color.CYAN;
    public final static Color NEUTRAL_COLOR = Color.DARK_GRAY;

    public DisplayController(JFrame frame, GameController controller) {
        this.frame = frame;
        this.controller = controller;

        globalChat = "";
        frenchChat = "";
        britishChat = "";
        prussianChat = "";
        russianChat = "";
        ottomanChat = "";
        austrianChat = "";
        spanishChat = "";
    }

    public void setInitContentPane() {
        setContentPane(MAIN_SCREEN);
    }

    private void setContentPane(int screen) {
        switch (screen) {
            case MAIN_SCREEN: frame.setContentPane(mainScreen);
                break;
        }
    }

    public void init() {
        mainScreen = new MainScreen(controller, this);
        mainScreen.chat.chatChooser.setupCursors();
        setInitContentPane();
    }

    /**
     * Save the old nation chat window. Change to the new nation chat window.
     * Update the chatChooser.
     *
     * @param oldNation - nation chat window user was viewing.
     * @param newNation - nation chat window user is now viewing.
     */
    public void changeChatNation(int oldNation, int newNation) {
        mainScreen.chat.chatWindow.changeChatNation(newNation);
        mainScreen.chat.chatChooser.setChatNation(oldNation, newNation);
        refresh();
    }

    public void saveChatWindow(int nation, String log) {
//        if (log.length() > ) todo max length of log file?

        switch (nation) {
            case Nation.GLOBAL: globalChat = log; break;
            case Nation.FRANCE: frenchChat = log; break;
            case Nation.GREAT_BRITAIN: britishChat = log; break;
            case Nation.PRUSSIA: prussianChat = log; break;
            case Nation.RUSSIA: russianChat = log; break;
            case Nation.OTTOMANS: ottomanChat = log; break;
            case Nation.AUSTRIA_HUNGRY: austrianChat = log; break;
            case Nation.SPAIN: spanishChat = log; break;
        }
    }

    public String loadChatWindow(int nation) {
        switch (nation) {
            case Nation.GLOBAL: return globalChat;
            case Nation.FRANCE: return frenchChat;
            case Nation.GREAT_BRITAIN: return britishChat;
            case Nation.PRUSSIA: return prussianChat;
            case Nation.RUSSIA: return russianChat;
            case Nation.OTTOMANS: return ottomanChat;
            case Nation.AUSTRIA_HUNGRY: return austrianChat;
            case Nation.SPAIN: return spanishChat;
        }
        return "Could not find chat log related to nation: " + nation;
    }

    public void receiveChatMsg(int fromNation, String name, String msg) {
        //Chat window stuff..
        addChatMsg(fromNation, name, msg);

        //Chat Chooser stuff..
        mainScreen.chat.chatChooser.messageReceived(fromNation);
    }

    public void addChatMsg(int nation, String name, String msg) {
        switch (nation) {
            case Nation.GLOBAL:
                if (globalChat.length() > 0)
                    globalChat = globalChat.concat("\n");
                globalChat = globalChat.concat(name + ": " + msg);
                break;
            case Nation.FRANCE:
                if (frenchChat.length() > 0)
                    frenchChat = frenchChat.concat("\n");
                frenchChat = frenchChat.concat(name + ": " + msg);
                break;
            case Nation.GREAT_BRITAIN:
                if (britishChat.length() > 0)
                    britishChat = britishChat.concat("\n");
                britishChat = britishChat.concat(name + ": " + msg);
                break;
            case Nation.PRUSSIA:
                if (prussianChat.length() > 0)
                    prussianChat = prussianChat.concat("\n");
                prussianChat = prussianChat.concat(name + ": " + msg);
                break;
            case Nation.RUSSIA:
                if (russianChat.length() > 0)
                    russianChat = russianChat.concat("\n");
                russianChat = russianChat.concat(name + ": " + msg);
                break;
            case Nation.OTTOMANS:
                if (ottomanChat.length() > 0)
                    ottomanChat = ottomanChat.concat("\n");
                ottomanChat = ottomanChat.concat(name + ": " + msg);
                break;
            case Nation.AUSTRIA_HUNGRY:
                if (austrianChat.length() > 0)
                    austrianChat = austrianChat.concat("\n");
                austrianChat = austrianChat.concat(name + ": " + msg);
                break;
            case Nation.SPAIN:
                if (spanishChat.length() > 0)
                    spanishChat = spanishChat.concat("\n");
                spanishChat = spanishChat.concat(name + ": " + msg);
                break;
        }
        
        mainScreen.chat.chatWindow.messageReceived(nation);

        mainScreen.getPanelHeightWidths();
    }

    public void showCommCard(int nation) {
        System.out.println("show CommCard " + nation);
        if ( commFrame != null ) {
            commScreenX = commFrame.getX();
            commScreenY = commFrame.getY();
            commScreenCoordinates = true;
            commFrame.dispose();
        }
        commFrame = new CommCardFrame(nation);

        if (commScreenCoordinates) {
            commFrame.setLocation(commScreenX, commScreenY);
        } else {
            int x = frame.getWidth()/2 - commFrame.getWidth()/2;
            int y = frame.getHeight()/2 - commFrame.getWidth()/2;
            x = x <= 0 ? 0 : x;
            y = y <= 0 ? 0 : y;

            commFrame.setLocation(x,y);
        }
    }

    public void showNationSummary(int nation) {
        System.out.println("show NationInfoFrame " + nation);
        if (nationSummaryFrame != null) {
            nationSummX = nationSummaryFrame.getX();
            nationSummY = nationSummaryFrame.getY();
            nationSummCoordinates = true;
            nationSummaryFrame.dispose();
        }
        nationSummaryFrame = new NationInfoFrame(controller, controller.getNationInstance(nation));

        if (nationSummCoordinates) {
            nationSummaryFrame.setLocation(nationSummX, nationSummY);
        } else {
            int x = frame.getWidth()/2 - nationSummaryFrame.getWidth()/2;
            int y = frame.getHeight()/2 - nationSummaryFrame.getWidth()/2;
            x = x <= 0 ? 0 : x;
            y = y <= 0 ? 0 : y;

            nationSummaryFrame.setLocation(x,y);
        }
        nationSummaryFrame.pack();
    }

    public void showPlacePanel() { mainScreen.sidePanel.pPanel.setVisible(true); }
    public void hidePlacePanel() { mainScreen.sidePanel.pPanel.setVisible(false);}

    public void refreshTerritoryInfo() {
        mainScreen.sidePanel.tiPanel.refreshTerritoryInfo();
    }

    public Component getMainScreen() {
        return mainScreen;
    }

    public Component getMap() {
        return mainScreen.map;
    }

    public static Color getNationColor(int nation) {
        switch (nation) {
            case 0: return DisplayController.NEUTRAL_COLOR;
            case 1: return DisplayController.FRENCH_COLOR;
            case 2: return DisplayController.BRITISH_COLOR;
            case 3: return DisplayController.PRUSSIAN_COLOR;
            case 4: return DisplayController.RUSSIAN_COLOR;
            case 5: return DisplayController.OTTOMAN_COLOR;
            case 6: return DisplayController.AUSTRIAN_COLOR;
            case 7: return DisplayController.SPANISH_COLOR;
            default: return DisplayController.NEUTRAL_COLOR;
        }
    }

    /**
     * Refresh the display of the frame.
     */
    public void refresh() {
        frame.validate();
        frame.repaint();
    }


    private JFrame frame;
    private MainScreen mainScreen;
    private JFrame commFrame;
    private JFrame nationSummaryFrame;

    private GameController controller;

    private String globalChat;
    private String frenchChat;
    private String britishChat;
    private String prussianChat;
    private String russianChat;
    private String ottomanChat;
    private String austrianChat;
    private String spanishChat;

    private final int MAIN_SCREEN = 0;

    private boolean commScreenCoordinates;
    private boolean nationSummCoordinates;
    private int commScreenX = -1;
    private int commScreenY = -1;
    private int nationSummX = -1;
    private int nationSummY = -1;
}